package com.pethospital.controller;

import com.pethospital.common.Result;
import com.pethospital.dto.StaffLoginDTO;
import com.pethospital.dto.StaffRegisterDTO;
import com.pethospital.entity.Staff;
import com.pethospital.service.StaffService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/staff")
public class StaffController {

    @Resource
    private StaffService staffService;
    
    @Resource
    private com.pethospital.util.FileUploadUtil fileUploadUtil;

    /**
     * 医护人员注册
     */
    @PostMapping("/register")
    public Result<Void> register(@RequestBody @Valid StaffRegisterDTO registerDTO) {
        staffService.register(registerDTO);
        return Result.success();
    }

    /**
     * 医护人员登录
     */
    @PostMapping("/login")
    public Result<Map<String, Object>> login(@RequestBody @Valid StaffLoginDTO loginDTO) {
        Map<String, Object> result = staffService.login(loginDTO);
        return Result.success(result);
    }

    /**
     * 获取当前医护人员信息
     */
    @GetMapping("/info")
    public Result<Staff> info(HttpServletRequest request) {
        Long staffId = (Long) request.getAttribute("userId");
        Staff staff = staffService.getById(staffId);
        return Result.success(staff);
    }

    /**
     * 更新医护人员信息
     */
    @PutMapping("/info")
    public Result<Void> updateInfo(@RequestBody Staff staff, HttpServletRequest request) {
        Long staffId = (Long) request.getAttribute("userId");
        staff.setId(staffId);
        staffService.updateById(staff);
        return Result.success();
    }

    /**
     * 上传头像
     */
    @PostMapping("/avatar")
    public Result<String> uploadAvatar(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        Long staffId = (Long) request.getAttribute("userId");
        String avatarUrl = fileUploadUtil.uploadFile(file, "avatar");
        
        // 更新医护人员头像
        Staff staff = new Staff();
        staff.setId(staffId);
        staff.setAvatar(avatarUrl);
        staffService.updateById(staff);
        
        return Result.success(avatarUrl);
    }
} 